using Vin.Extension.Redis.Cache;
using Vin.Tool.Core.DateTimeCategory;

namespace Vin.Extension.Redis.Tools;

public class UniqueNumberGenerateTool
{
    // 时间唯一号自增Key
    public const string DateNoIncrKey = "Unique:Number:Generate:Incr:";

    // 时间唯一号自增时间（秒）
    public const int DateNoSecond = 3;

    /// <summary>
    /// 生成时间唯一订单号
    /// </summary>
    /// <returns></returns>
    public static async Task<string> GenerateDateNoAsync(int padLeftWidth = 4, char paddingChar = '0')
    {
        var now = DateTime.Now;
        var num = await RedisCacheManager.GetRedisCache()
            .IncrAsync(DateNoIncrKey + now.ToTimestamp(false),
                TimeSpan.FromSeconds(DateNoSecond));
        return now.ToString("yyyyMMddHHmmss") + num.ToString().PadLeft(padLeftWidth, paddingChar);
    }
}